import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

import 'MyCustomController.dart';

class VideoView extends StatefulWidget {
  String url;
  String cover;

  VideoView( {required this.url,required this.cover});

  @override
  _VideoViewState createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;
  bool _isControllerInitialized = false;
  @override
  void initState() {
    super.initState();
    // 初始化视频控制器
    _videoPlayerController = VideoPlayerController.network(widget.url)
      ..initialize().then((_) {
        setState(() {
          _isControllerInitialized = true;
        });
      });
    _videoPlayerController.play();
    _videoPlayerController.addListener(() {
      if (_videoPlayerController.value.hasError) {
        // 处理错误
      } else if (_videoPlayerController.value.position >= _videoPlayerController.value.duration) {
        // 视频播放完成，重新播放
        _videoPlayerController.seekTo(Duration.zero);
        _videoPlayerController.play();
      }
      setState(() {
        _videoPlayerController.value.isPlaying;
      });
    });
  }

  bool computedWidthHeight() {
    // double aspectRatio = _videoPlayerController!.value.aspectRatio;
    // double? vWidth = context.size?.width;
    if (_videoPlayerController != null &&
        _videoPlayerController!.value.size != null &&
        _videoPlayerController!.value.size!.width >
            _videoPlayerController!.value.size!.height) {
      return true;
      // 视频是横屏的
    } else {
      // 视频是竖屏的
      return false;
    }
  }

  @override
  void dispose() {
    // 释放资源
    _videoPlayerController.dispose();
    // _chewieController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    if (_isControllerInitialized && !computedWidthHeight()) {
      // 当 _chewieController 已被初始化时，显示视频播放器
      return Container(
          child:Stack(
            children: [

              Center(
                  child: SizedBox.expand(
                      child: FittedBox(
                          fit: BoxFit.cover,
                          child:SizedBox(
                            height: _videoPlayerController.value.size.height,
                            width: _videoPlayerController.value.size.width,
                            child:
                            VideoPlayer(_videoPlayerController!),
                          )
                      )
                  )),
              MyCustomController(_videoPlayerController),
              !_videoPlayerController.value.isPlaying? Positioned(
              child:Center(
              child:Icon(Icons.play_arrow, color: Colors.red,) ,
              )
              ):Text('')
                        ,
            ],

          )
      );

    } else if(_isControllerInitialized && computedWidthHeight()){
      return Stack(
        alignment: Alignment.center, // 确保Stack中的内容居中
        children: [
          Center(
            child: _videoPlayerController != null
                ?
            // Expanded(
            // child:
            AspectRatio(
              aspectRatio: _videoPlayerController!.value.aspectRatio,
              child: VideoPlayer(_videoPlayerController!),
            )
            // )
                : Container(),
          ),
          MyCustomController(_videoPlayerController),
          !_videoPlayerController.value.isPlaying? Positioned(
              child:Center(
                child:Icon(Icons.play_arrow, color: Colors.red,) ,
              )
          ):Text(''),

        ],
      );
    } else {
      return Container(
        width: double.infinity,
        height: double.infinity,

      );
    };
  }

}
